package com.yksj.healthtalk.adapter;

import java.util.ArrayList;

import org.universalimageloader.core.DefaultConfigurationFactory;
import org.universalimageloader.core.DisplayImageOptions;
import org.universalimageloader.core.ImageLoader;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.yksj.healthtalk.entity.ShopListItemEntity;
import com.yksj.healthtalk.ui.R;

/**
 * 购物车
 * 
 * @author Administrator
 * 
 */
public class TrolleyAdapter extends BaseAdapter{

	private LayoutInflater inflater;
	private Context context;
	private ViewHolder holder;
	private OnClickListener listener;
	private ArrayList<ShopListItemEntity> entities;
	private TrolleyListLisenter mTrolleyListLisenter;
	private ImageLoader loader;
	private  DisplayImageOptions mUnknowDisplayImageOptions;

	public TrolleyAdapter(Context context,
			ArrayList<ShopListItemEntity> entities) {
		// TODO Auto-generated constructor stub
		this.context = context;
		inflater = LayoutInflater.from(context);
		this.entities = entities;
		loader = ImageLoader.getInstance();
		mUnknowDisplayImageOptions = DefaultConfigurationFactory.createUnknowShopImageOptions(context);
		if (context instanceof OnClickListener) {
			this.listener = (OnClickListener) context;
		}
		if (context instanceof TrolleyListLisenter) {
			this.mTrolleyListLisenter = (TrolleyListLisenter) context;
		}
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return entities.size();
	}
	
	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.server_center_trolley_item,
					null);
			holder.mDeleteBtn = (Button) convertView.findViewById(R.id.delete);
			holder.mMinusBtn = (Button) convertView.findViewById(R.id.minus);
			holder.mAddBtn = (Button) convertView.findViewById(R.id.add);
			// holder.mMinusBtn = (Button)convertView.findViewById(R.id.minus);
			holder.mNumEt = (EditText) convertView.findViewById(R.id.num);
			holder.mNameTv = (TextView) convertView.findViewById(R.id.name);
			holder.mSpecificationsTv = (TextView) convertView
					.findViewById(R.id.specifications);
			holder.mShopTv = (TextView) convertView.findViewById(R.id.shop);
			holder.mVipTv = (TextView) convertView.findViewById(R.id.vip);
			holder.mAddressTv = (TextView) convertView
					.findViewById(R.id.address);
			holder.header = (ImageView)convertView.findViewById(R.id.header);
			holder.mState = (TextView)convertView.findViewById(R.id.state);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		final ShopListItemEntity entity = entities.get(position);
		
		loader.displayImage(entity.getPICTURE_ADDR(),holder.header,mUnknowDisplayImageOptions);

		// 商品名称
		holder.mNameTv.setText(entity.getGOODS_NAME());
		// 商品规格
		holder.mSpecificationsTv.setText("商品规格: " + entity.getSPECIFICATIONS());
		// 市场价格
		holder.mShopTv.setText("市场价格: ￥" + entity.getSHOP_PRICE());
		// 普通会员
		holder.mVipTv.setText("普通会员: ￥" + entity.getVIP_PRICE());
		// 生产厂家
		holder.mAddressTv.setText(entity.getFACTORY());
		// 数量
		holder.mNumEt.setText(entity.getGOODS_COUNT() + "");
		//是否下架
		if (!entity.getSHELVES_FLAG().equals("0")) {
			holder.mState.setText("");
		}else {
			holder.mState.setText("已下架");
		}
		
		holder.mDeleteBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (listener != null) {
					v.setTag(position);
					listener.onClick(v);
				}
			}
		});

		holder.mAddBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (mTrolleyListLisenter != null) {
					mTrolleyListLisenter.addCount(position, entity,
							entity.getGOODS_COUNT(),entity.getGOODS_COUNT()+1);
					entity.setGOODS_COUNT(entity.getGOODS_COUNT()+1);
					notifyDataSetChanged();
				}
			}
		});

		holder.mMinusBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (mTrolleyListLisenter != null) {
					int count;
					if (entity.getGOODS_COUNT()-1 <= 0) {
						 count =entity.getGOODS_COUNT();
					}else {
						count =entity.getGOODS_COUNT()-1;
					}
					mTrolleyListLisenter.minusCount(position, entity,
							entity.getGOODS_COUNT(),count);
					entity.setGOODS_COUNT(count);
					notifyDataSetChanged();
				}
			}
		});
		return convertView;
	}

	public class ViewHolder {
		ImageView header;
		Button mDeleteBtn;
		Button mMinusBtn;
		Button mAddBtn;
		EditText mNumEt;
		TextView mNameTv;
		TextView mSpecificationsTv;
		TextView mShopTv;
		TextView mVipTv;
		TextView mAddressTv;
		TextView mState;//是否下架
	}

	public interface TrolleyListLisenter {
		void addCount(int position, ShopListItemEntity entity,
				int beforeChangeCount,int endChangeCount);

		void minusCount(int position, ShopListItemEntity entity,
				int beforeChangeCount,int endChangeCount);
	}

}
